package object2;


import java.lang.Override;

/**
 * 面向对象编程(中级部分)练习
 */
public class Object2Exercise07 {

    public static void main(String[] args) {

        /*
            1)在BankAccount类的基础上扩展新类 CheckingAccount 对每次存款和取款都收取1美元的手续费
            2)扩展BankAccount类，新类SavingAccount每个月都有利息产生(earnMonthlyInterest 方法被调用)
              并且有每月三次免手续费的存款或取款。在earnMonthlyInterest方法中重置交易计数
            3)体会重写的好处
         */
        CheckingAccount checkingAccount = new CheckingAccount(1000.0);
        checkingAccount.deposit(10.0);//1009
        checkingAccount.withdraw(5.0);//1009 - 1 - 5 = 1003
        System.out.println(checkingAccount.getBalance());

        SavingAccount savingAccount = new SavingAccount(1000.0, 50.0);
        savingAccount.deposit(10.0);//1000 + 10 = 1010
        savingAccount.withdraw(5.0);//1010 - 5 = 1005
        savingAccount.withdraw(10.0);//1005 - 10 = 995
        savingAccount.deposit(10.0);//995 + 10 - 1 = 1004
        savingAccount.earnMonthlyInterest();//1004 + 50 - 1 = 1053
        savingAccount.deposit(20.0);//1053 + 20 = 1073
        System.out.println(savingAccount.getBalance());
    }
}

class SavingAccount extends CheckingAccount {

    private int chance = 3;

    private double interest;

    public SavingAccount(double initialBalance, double interest) {
        super(initialBalance);
        this.interest = interest;
    }

    public void earnMonthlyInterest() {
        super.deposit(interest);//利息产生相当于存钱
        this.chance = 3;
        System.out.println("存入利息...重置计数...");
    }

    @Override
    public void deposit(double amount) {
        if (this.chance > 0) {
            double balance = super.getBalance();
            super.setBalance(balance += amount);
            chance--;
            System.out.println("还有" + this.chance + "次机会免手续费");
        } else {
            super.deposit(amount);
        }

    }

    @Override
    public void withdraw(double amount) {
        if (this.chance > 0) {
            double balance = super.getBalance();
            super.setBalance(balance -= amount);
            chance--;
            System.out.println("还有" + this.chance + "次机会免手续费");
        } else {
            super.withdraw(amount);
        }
    }

    public int getChance() {
        return chance;
    }

    public void setChance(int chance) {
        this.chance = chance;
    }

    public double getInterest() {
        return interest;
    }

    public void setInterest(double interest) {
        this.interest = interest;
    }
}

class CheckingAccount extends BankAccount {

    public CheckingAccount(double initialBalance) {
        super(initialBalance);
    }

    @Override
    public void deposit(double amount) {
        super.deposit(amount - 1);
    }

    @Override
    public void withdraw(double amount) {
        super.withdraw(amount + 1);
    }
}

class BankAccount{

    private double balance;//余额

    public BankAccount(double initialBalance) {
        this.balance = initialBalance;
    }

    //存款
    public void deposit(double amount) {
        balance += amount;
    }

    //取款
    public void withdraw(double amount) {
        balance -= amount;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }
}

